package com.cpit.platform.tool.templet.web;

import com.cpit.platform.common.dao.SimpleExpression;
import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.tool.templet.entity.TempletEntity;
import com.cpit.platform.tool.templet.service.TempletEntityService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/TempletEntity")
public class TempletEntityController {
    /**
     * 实体类显示名称
     */
    private final String ENTITY_NAME = TempletEntity.ENTITY_DISPLAY_NAME;

    @Autowired
    private TempletEntityService entityService;

    @ApiOperation(value="查询所有" + ENTITY_NAME, notes="无条件查询所有记录")
    @RequestMapping(value="/queryAll", method = RequestMethod.GET)
    public List<TempletEntity> queryAll() {
        return this.entityService.findAll();
    }

    @ApiOperation(value = "查询" + ENTITY_NAME+"(Map条件)", notes = "通过Map传递对象属性条件查询所有符合条件的记录，支持模糊查询。")
    @ApiImplicitParam(name = "data", value = ENTITY_NAME + "所包含的属性名值对组成的Map", required = true, dataType = "Map")
    @RequestMapping(value = "/queryByConditionMap", method = RequestMethod.POST)
    public Result<List<TempletEntity>> queryByConditionMap(@RequestBody Map<String, Object> data) {
        return new Result<>(Result.CODE_SUCCESS, "", this.entityService.findByQueryCondition(data));
    }

    @ApiOperation(value = "分页查询" + ENTITY_NAME+"(Map条件)", notes = "通过分页条件和Map传递对象属性条件查询所有符合条件的记录，支持模糊查询。")
    @ApiImplicitParam(name = "data", value = ENTITY_NAME + "所包含的属性名值对组成的Map", required = true, dataType = "Map")
    @RequestMapping(value = "/queryPageByConditionMap", method = RequestMethod.POST)
    public Result<List<TempletEntity>> queryPageByConditionMap(
            @ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = false, defaultValue = "20") int size,
            @ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order,
            @ApiParam(value = "排序字段，与对应entity中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
            @RequestBody Map<String, Object> data) {
        return new Result<>(Result.CODE_SUCCESS, "", this.entityService.findByQueryCondition(page, size, order, orderBy, data).getContent());
    }

    @ApiOperation(value = "查询" + ENTITY_NAME+"(SimpleExpression列表条件)", notes = "通过由SimpleExpression组成的List条件查询所有符合条件的记录，支持模糊查询。")
    @ApiImplicitParam(name = "data", value = "由SimpleExpression组成的List", required = true, dataType = "List")
    @RequestMapping(value = "/queryBySimpleExpressionList", method = RequestMethod.POST)
    public Result<List<TempletEntity>> queryBySimpleExpressionList(@RequestBody List<SimpleExpression> data) {
        return new Result<>(Result.CODE_SUCCESS, "", this.entityService.findByQueryCondition(data));
    }

    @ApiOperation(value = "分页查询" + ENTITY_NAME+"(SimpleExpression列表条件)", notes = "通过分页条件和由SimpleExpression组成的List条件查询所有符合条件的记录，支持模糊查询。")
    @ApiImplicitParam(name = "data", value = "由SimpleExpression组成的List", required = true, dataType = "List")
    @RequestMapping(value = "/queryPageBySimpleExpressionList", method = RequestMethod.POST)
    public Result<List<TempletEntity>> queryPageBySimpleExpressionList(
            @ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = false, defaultValue = "20") int size,
            @ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order,
            @ApiParam(value = "排序字段，与对应entity中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
            @ApiParam(value = "由SimpleExpression组成的List。") @RequestBody List<SimpleExpression> data) {
        return new Result<>(Result.CODE_SUCCESS, "", this.entityService.findByQueryCondition(page, size, order, orderBy, data).getContent());
    }

    @ApiOperation(value = "分页查询" + ENTITY_NAME+"(TableDataQueryCondition条件)", notes = "通过TableDataQueryCondition条件查询，查询条件为:queryCond的Map条件或queryCondSimple的List<SimpleExpression>条件，或二者组合查询。返回jqGrid分页JSON形式，支持模糊查询。")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
    })
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/queryPageByTableQueryCondition", method = RequestMethod.GET)
    public TableJsonData queryPageByTableQueryCondition( @ApiParam(value ="TableDataQueryCondition分页条件和其他业务条件，业务查询条件可为:queryCond的Map条件或queryCondSimple的List<SimpleExpression>条件，或二者组合查询")TableDataQueryCondition cond) {
        return this.entityService.findByTableQueryCondition(cond);
    }

    @ApiOperation(value="查询" + ENTITY_NAME, notes="通过ID查询符合条件的记录")
    @RequestMapping(value="/{id}", method = RequestMethod.GET)
    public Result<TempletEntity> queryById(@ApiParam(value = ENTITY_NAME + "ID") @PathVariable(value = "id") Long id) {
        return new Result<>(Result.CODE_SUCCESS, "", this.entityService.findById(id));
    }

    @ApiOperation(value="创建" + ENTITY_NAME, notes="根据界面录入的" + ENTITY_NAME + "创建")
    @ApiImplicitParam(name = "data", value = ENTITY_NAME, required = true, dataType = "TempletEntity")
    @RequestMapping(value="/create", method = RequestMethod.POST)
    public Result<TempletEntity> create(@RequestBody TempletEntity data) {
        return this.entityService.create(data);
    }

    @ApiOperation(value="修改" + ENTITY_NAME, notes="根据界面录入的" + ENTITY_NAME + "修改")
    @ApiImplicitParam(name = "data", value = ENTITY_NAME, required = true, dataType = "TempletEntity")
    @RequestMapping(value="/modify", method = RequestMethod.POST)
    public Result<TempletEntity> modify(@RequestBody TempletEntity data) {
        return this.entityService.modify(data);
    }

    @ApiOperation(value="删除" + ENTITY_NAME, notes="删除给定ID的" + ENTITY_NAME)
    @RequestMapping(value="/delete/{id}", method = RequestMethod.DELETE)
    public Result<TempletEntity> delete(@ApiParam(value = ENTITY_NAME + "ID", required = true) @PathVariable("id") Long id) {
        TempletEntity data = new TempletEntity();
        data.setId(id);
        
        return this.entityService.delete(data);
    }

    @ApiOperation(value = "导出" + ENTITY_NAME + "列表", notes = "将系统中的" + ENTITY_NAME + "导出到excel文件。")
    @RequestMapping(value = {"/export"}, method = RequestMethod.GET)
    public String export(HttpServletRequest request, HttpServletResponse response) {
        return this.entityService.export(request,response);
    }


    @ApiOperation(value="导入" + ENTITY_NAME + "列表", notes="向系统中导入" + ENTITY_NAME + "列表")
    @ResponseBody
    @RequestMapping(value = "/fileImport", method = RequestMethod.POST, consumes = "multipart/form-data")
    public Result<Integer> fileImport(
            @ApiParam(value = ENTITY_NAME + "列表", required = true) @RequestParam("fileupload") MultipartFile file, HttpServletRequest request) {
        return this.entityService.fileImport( file, request);
    }

}
